﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Telerik.Web.UI;
using HandBook.Data;
using HandBook.Common;

public partial class Admin_FoodBookManagement : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (ViewState["holdstate"] != null)
        {
            uxFoodBook.DataSource = ViewState["holdstate"];
        }
    }
    protected void uxFoodBook_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        ReadAllFoodBook();
    }

    private void ReadAllFoodBook()
    {
        var foodList = from foodBook in HandBookData.EatDrinkDB.FoodBooks
                       select foodBook;
        uxFoodBook.DataSource = foodList;
    }
    
    protected void uxFoodBook_CancelCommand(object source, GridCommandEventArgs e)
    {
        ReadAllFoodBook();
    }
    protected void uxFoodBook_DeleteCommand(object source, GridCommandEventArgs e)
    {
        GridEditableItem editedItem = e.Item as GridEditableItem;
        int foodBookID = Convert.ToInt32(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["FoodBookID"]);
        var food = HandBookData.EatDrinkDB.FoodBooks.Single(f => f.FoodBookID == foodBookID);
        HandBookData.EatDrinkDB.FoodBooks.DeleteOnSubmit(food);
        HandBookData.EatDrinkDB.SubmitChanges();
        uxFoodBook.Rebind();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        EatDrinkDBDataContext context = new EatDrinkDBDataContext();
        Label1.Visible = false;
        string content = txtSearch.Text.Trim();
        if (content == "Nhập Tên Món Ăn Cần Tìm...")
        {
            Label1.Visible = true;
            ReadAllFoodBook();
        }
        else
        {
            string specialChar = "-_+*:,.@{}[]()~$#%^&=|;?/<>!";
            int len = specialChar.Length;
            specialChar = specialChar.Trim();

            //lọc bỏ các ký tự đặc biệt
            for (int i = 0; i < len; i++)
            {
                if (content.Contains(specialChar[i]))
                {
                    content = content.Replace(specialChar[i].ToString(), "");
                }
            }
            //tách chuỗi tìm kiếm
            string[] keywords = content.Split(' ');
            string where = string.Empty;
            foreach (string item in keywords)
            {
                if (where.Equals(string.Empty))
                {
                    where = "Title LIKE '%" + item + "%'";
                }
                else
                {
                    where += " OR Title LIKE '%" + item + "%'";
                }
            }
            string sql = "SELECT * FROM FoodBook WHERE " + where;

            var foodbookList = context.ExecuteQuery<FoodBook>(sql);

            DataTable table = new DataTable();
            table.Columns.Add("FoodBookID");
            table.Columns.Add("Title");
            table.Columns.Add("ShortDescription");
            table.Columns.Add("Image");
            table.Columns.Add("IsShowHomePage");

            foreach (var item in foodbookList)
            {
                DataRow row = table.NewRow();
                row["FoodBookID"] = item.FoodBookID;
                row["Title"] = item.Title;
                row["ShortDescription"] = item.ShortDescription;
                row["Image"] = item.Image;
                row["IsShowHomePage"] = item.IsShowHomePage;
                table.Rows.Add(row);
            }
            ViewState["holdstate"] = table;
            uxFoodBook.DataSource = table;
            uxFoodBook.Rebind();
        }
    }
    protected void btRefreshGrid_Click(object sender, EventArgs e)
    {
        uxFoodBook.Rebind();
    }
}
